package com.love.poetry.infrastructure;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.youboy.util.AssertUtils;

public class SpringBeanHolder implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext;

	public final void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringBeanHolder.awareApplicationContext(applicationContext);
	}

	private static void awareApplicationContext(ApplicationContext applicationContext) {
		AssertUtils.notNull(applicationContext);
		SpringBeanHolder.applicationContext = applicationContext;
	}

	public static <T> T getBean(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}

	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}

}